let sql = {};

sql.testsql = function(arg){
    cc.log('testsql other');
    let db = new jsb.SQLWrapper();
    //let url = jsb.fileUtils.fullPathForFilename('equip.db');
    //let path = jsb.fileUtils.getWritablePath()
    let url = db.getPath('equip.db','res/raw-assets/resources');
    let isopen = db.open(url);//"res/raw-assets/resources/equip.db");
    cc.log('url: '+url)
    //cc.log('writablePath'+path)
    //var searchPaths = jsb.fileUtils.getSearchPaths();
    //cc.log(searchPaths)
    //cc.log("isopen",isopen);

    if(isopen){
        cc.log("db open success")

        let st = db.exec('select id,name,desc,star,icon from common_equipment_info');
        while(st.nextRow()){
            //cc.log(st);
            // cc.log('',st.valueCount(),st.valueInt(0),st.valueString(1),st.valueInt(3),st.valueName(0),st.dataType(0),st.dataType(1))
            let obj = sql.convertObj(st);
            cc.log(JSON.stringify(obj))
        }

        st = db.exec('select id,name,icon from common_equipment_info');
        while(st.nextRow()){
            //cc.log(st);
            // cc.log('',st.valueCount(),st.valueInt(0),st.valueString(1),st.valueInt(3),st.valueName(0),st.dataType(0),st.dataType(1))
            let obj = sql.convertObj(st);
            cc.log(JSON.stringify(obj))
        }
    }else{
        cc.log("db open failed")
    }
    return isopen;
};

//only int,string
sql.convertObj = function(st){
    let obj = {};
    let count = st.valueCount();
    for (var index = 0; index < count; index++) {
        let type = st.dataType(index);
        if (type == 1) {
            obj[st.valueName(index)] = st.valueInt(index)
        } else if (type == 3) {
            obj[st.valueName(index)] = st.valueString(index)
        }
    }

    return obj;
};

sql.convertArr = function(st){
    let arr = [];
    while(st.nextRow()){
        let obj = sql.convertObj(st);
        arr.push(arr);
    }
    return arr;
}

module.exports = sql;